package com.cz.znmes;

import java.util.Date;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicInteger;

import com.cz.znmes.dto.AddProductionPlanDTO;
import com.cz.znmes.dto.ResponseDTO;

import cn.hutool.core.thread.ThreadUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.json.JSONUtil;

public class AddPlan {
    
    public static void main(String[] args) {
        // 示例：发送POST请求并携带token
        String url = "http://mes-service.sade.ant2world.com/production-plan/addProductionPlan";
        String token = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJleHAiOjE3NTAwNzUwMDcsInVzZXJuYW1lIjoiYWRtaW4ifQ.G3O5GWBrs61PNoHB11MghfaEJoGNbGikVAx_AmaS--Q";
        

        int totalRequests = 100;
        CountDownLatch latch = new CountDownLatch(totalRequests);
        AtomicInteger successCount = new AtomicInteger(0);
        AtomicInteger failCount = new AtomicInteger(0);

        // 创建线程池并执行并发请求
        ThreadUtil.execAsync(() -> {
            for (int i = 0; i < totalRequests; i++) {
                final int index = i;
                ThreadUtil.execute(() -> {
                    try {
                        AddProductionPlanDTO addProductionPlanDTO = new AddProductionPlanDTO();
                        addProductionPlanDTO.setMaterialCode("YLBCP0001");
                        addProductionPlanDTO.setMaterialName("殷亮半成品");
                        addProductionPlanDTO.setScheduleDate(new Date());
                        addProductionPlanDTO.setPlannedQuantity(100);
                        addProductionPlanDTO.setStatus(1);

                        // 发送POST请求
                        HttpResponse response = HttpRequest.post(url)
                                .header("Authorization", "Bearer " + token)
                                .header("Content-Type", "application/json")
                                .body(JSONUtil.toJsonStr(addProductionPlanDTO))
                                .execute();

                        // 获取响应
                        if (response.isOk()) {
                            String result = response.body();
                            ResponseDTO responseDTO = JSONUtil.toBean(result, ResponseDTO.class);
                            if (responseDTO.getCode() == 200) {
                                successCount.incrementAndGet();
                            } else {
                                failCount.incrementAndGet();
                            }
                        }
                    } catch (Exception e) {
                        System.out.println("Request " + index + " error: " + e.getMessage());
                        failCount.incrementAndGet();
                    } finally {
                        latch.countDown();
                    }
                });
            }
        });

        try {
            // 等待所有请求完成
            latch.await();
            System.out.println("All requests completed!");
            System.out.println("Success count: " + successCount.get());
            System.out.println("Fail count: " + failCount.get());
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
